require_relative "twikit_tokenizer"

class TwikitTokenizerUI
	def initialize
    puts "Special instructions:: T1=> #{@T1 = "INDEX test_bin/ ^.*test.*\.rb$"}"
    # puts "Special instructions:: T2=> #{@T2 = "INDEX test_bin/ ^((?!test).)*\.rb$"}"
    puts "Special instructions:: T3=> #{@T3 = "INDEX0 . ^.*test.*\.rb$"}"
    # puts "Special instructions:: T4=> #{@T4 = "INDEX0 . ^((?!test).)*\.rb$"}"
	  puts "Special instructions:: T5=> #{@T5 = "INDEX C:/Ruby193/lib/ruby/gems/1.9.1/gems/ ^.*test.*\.rb$"}"
    # puts "Special instructions:: T6=> #{@T6 = "INDEX C:/Ruby193/lib/ruby/gems/1.9.1/gems/ ^((?!test).)*\.rb$"}"
    puts "Special instructions:: T7=> #{@T7 = "INDEX0 C:/Ruby193/lib/ruby/gems/1.9.1/gems/mocha-0.10.3/test/unit/ ^.*test.*\.rb$"}"
    # puts "Special instructions:: T8=> #{@T8 = "INDEX C:/Ruby193/lib/ruby/gems/1.9.1/gems/mocha-0.10.3/lib/ ^((?!test).)*\.rb$"}"
    puts "Special instructions:: T9=> #{@T9 = "INDEX0 ../../dummy_codebase_1 ^.*\.rb$"}"
    puts "\n"
    puts "Please enter your unit tests, separated with ',' --OR-- 'INDEX path regexp' to index all unit tests containing regexp within path::"
    puts "(e.g. test_bin/SimpleAdderSecant/simple_adder_test.rb,test_file_1.tst)--OR--(e.g. INDEX test_bin/ ^.*_test.rb$, INDEX1 C:/Ruby193/ ^test_.*\.rb$)"
    tokenizer = TwikitTokenizer.new()
		filenames = convert_special_directives(gets.chomp)
		if(filenames[0..4].upcase == "INDEX")
		  unit_test_options = get_indexing_options(filenames)
		  puts "UT INDEXING OPTIONS:#{unit_test_options}"
      if(tokenizer.load_unit_tests_in_directory(unit_test_options['path'], unit_test_options['pattern'], unit_test_options['recursion']))then
        puts "\n\n"
        # puts "Please enter 'INDEX path regexp' to index all source files containing regexp within path::"
        # puts "(e.g. INDEX test_bin/ ^.*.*\.rb$, INDEX0 . ^((?!test).)*\.rb$)"
        #filenames = convert_special_directives(gets.chomp)
        #if(filenames[0..4].upcase == "INDEX")
        #  source_file_options = get_indexing_options(filenames.chomp)
        #  puts "SF INDEXING OPTIONS:#{source_file_options}"
        #  if(tokenizer.load_source_files_in_directory(source_file_options['path'], source_file_options['pattern'], source_file_options['recursion']))then
            puts "INDEXING..."
            tokenizer.index_loaded_unit_tests()
        #  else raise "There was an error while loading the source files based on your input options\n#{filenames}" end
        #else raise "You must use the INDEX switch to select your source files." end
      else raise "There was an error while loading the unit tests based on your input options\n#{filenames}" end
		else
  		unit_tests = filenames.chomp.split(",")
  		if(unit_tests.length > 0)then
        if(tokenizer.load_unit_tests( unit_tests ))then
          puts ".."
          puts "Please enter your source files, separated with ',':"
          puts "(e.g. SimpleAdderSecant/simple_adder.rb,test_file_2.tst)"
          # filename = gets
          # source_files = filename.chomp.split(",")
      		# if(source_files.length > 0) then
      		#	if(tokenizer.load_source_files( source_files ))then
               puts " -------------------------------------------------------------------"
               puts " -----------------TWIKIT TOKENIZER SECANT UI------------------------"
               puts " "
             unit_tests.each{|ut_fn|
               puts "===============>TOKEN INDEX for '#{ut_fn}:"
               puts tokenizer.get_token_index_for_unit_test(ut_fn)
               puts " "
               puts " "
        			 puts ":::::::::::::::>SOURCE INTERSECTIONS for '#{ut_fn}:"
        			 tokenizer.get_source_intersections_for_unit_test(ut_fn).each do |value| puts value end
               puts " "
               puts " "
               puts ":::::::::::::::>XTP Map for '#{ut_fn}:"
               tokenizer.get_XTP_map_for_unit_test(ut_fn)['xtp'].each do |index, value| puts "--" + index + "--"; puts value end
               puts " "
               puts " "
               puts "Saved XTP Map to '" + tokenizer.save_XTP_map_for_unit_test(ut_fn) + "'"
        	   }
      		#	else raise "There was an error while loading the source files that you provided" end
      		#else puts "No source file filenames found in you input" end
      	else raise "There was an error while loading the unit tests that you provided" end
    	else puts "No unit test filenames found in your input" end
  	end
  puts "-------------------------Goodbye!-------------------------"
  puts "----------------------------------------------------------"
  end
  
  def convert_special_directives( input_dir )
    if(input_dir.upcase === "T1")then input_dir = @T1 end
    if(input_dir.upcase === "T2")then input_dir = @T2 end
    if(input_dir.upcase === "T3")then input_dir = @T3 end
    if(input_dir.upcase === "T4")then input_dir = @T4 end
    if(input_dir.upcase === "T5")then input_dir = @T5 end
    if(input_dir.upcase === "T6")then input_dir = @T6 end
    if(input_dir.upcase === "T7")then input_dir = @T7 end
    if(input_dir.upcase === "T8")then input_dir = @T8 end
    if(input_dir.upcase === "T9")then input_dir = @T9 end
    return input_dir
  end
  
  def get_indexing_options(options_string)
    options = options_string.split(" ")
    if(options.length >= 1)then
      recursion = options[0].length > 5 ? options[0].slice!(5..options[0].length).to_i : -1
      path = options[1].nil? ? "." : options[1]
      path = path.gsub(/\\/, '/')
      if(options.length > 3)then ps=""; options[2..options.length].each{|p|ps+=p}; pattern = Regexp.new(ps)
      else pattern = options[2].nil? ? /.*\.rb/ : Regexp.new(options[2])
      end
      return {'recursion'=>recursion, 'path'=>path, 'pattern'=>pattern}
    end
  end
  
end

TwikitTokenizerUI.new